/*
 * DataBaseWindow.cpp
 *
 *  Created on: 25.03.2009
 *      Author: Konan Andrej
 */

//#include <QtGui>

#include "DataBaseWindow.h"

DataBaseWindow::DataBaseWindow(DataBase* db, QWidget *parent): QDialog( parent )
{
        DB = db;

	LastPressed = 0;

        AddButton = new QPushButton(tr("Add"));
	EditButton = new QPushButton(tr("Edit"));
	RemoveButton = new QPushButton(tr("Remove"));
        CloseButton = new QPushButton(tr("Close"));

	ActionButtonsLayout = new QVBoxLayout();
	ActionButtonsLayout->addWidget( AddButton );
	ActionButtonsLayout->addWidget( EditButton );
	ActionButtonsLayout->addWidget( RemoveButton );
        ActionButtonsLayout->addStretch();
        ActionButtonsLayout->addWidget( CloseButton );

	DBTable = new StockTableView( DB, this );

	TableAndButtonsLayout = new QHBoxLayout();
	TableAndButtonsLayout->addWidget( DBTable );
	TableAndButtonsLayout->addLayout( ActionButtonsLayout );

	DBDialogLayout = new QVBoxLayout();
	DBDialogLayout->addLayout( TableAndButtonsLayout );
	this->setLayout( DBDialogLayout );

        QVector<QString> headers;
        DB->GetColumnHeaderVector( headers );
        ItemDlg = new ItemEditDlg( headers, DB->GetFieldNumber(), DB->GetKeyField(), this );


	//Creating signals and slots connections
        connect( ItemDlg, SIGNAL( ItemMade(const ItemType&) ), this, SLOT( GetItem(const ItemType&) ) );

	connect( AddButton, SIGNAL( clicked() ), this, SLOT( AddPressed() ) );
	connect( EditButton, SIGNAL( clicked() ), this, SLOT( EditPressed() ) );
	connect( RemoveButton, SIGNAL( clicked() ), this, SLOT( RemovePressed() ) );
        connect( CloseButton, SIGNAL( clicked() ), this, SLOT( close() ) );

	//Print("End of DataBaseWindow constructor");
}

int DataBaseWindow::AddItem( const ItemType& item )
{
	//Print( "AddItem() is executed" );

	switch( DB->InsertItem( item ) ){
		case dup_error:
                        Output( warning, tr("Item with ") + DB->GetColumnHeader( DB->GetKeyField() )
                                +QString(": ") + item.Get( DB->GetKeyField() ) + tr(" already exists in database") );
			return dup_error;
			break;
		case ok:
			Output( message, tr("Item was succesfully inserted") );
                        emit ItemDlg->close();
			break;
	}

	return ok;
}

int DataBaseWindow::EditItem( const ItemType& item )
{
	//Print( "EditItem() is executed" );

	switch ( DB->EditItem( item ) ){
		case find_error:
			//means user has changed an article
			//because in other way ok result should be returned
			//new article is already exists in DB
			//Output( warning, tr("Item with Article: ") + item.GetArticle() + tr(" already exists in database") );
                        return find_error;
			break;
		case ok:
			Output( message, tr("Item was succesfully edited") );
                        emit ItemDlg->close();
			return ok;
			break;
		default:
			return ok;
	}
}


void DataBaseWindow::AddPressed()
{
	LastPressed = 1;

        ItemDlg->SetItem( ItemType( DB->GetFieldNumber() ) );
        ItemDlg->exec();

}

void DataBaseWindow::EditPressed()
{
	LastPressed = 2;

        ItemType item(DB->GetFieldNumber());
	switch( DBTable->GetSelectedItem( item ) ){
		case 1:
			//means no row is selected
			Output( warning, tr("No item is selected") );
			break;
		case 0:
			//item was succesfully read from DB                   
                        ItemDlg->SetItem( item );
                        ItemDlg->exec();
			break;
	}
}

void DataBaseWindow::RemovePressed()
{
        ItemType item(DB->GetFieldNumber());
	switch( DBTable->GetSelectedItem( item ) ){
		case 1:
			//means no row is selected
			Output( warning, tr("No item is selected") );
			break;
		case 0:
			//in item we have real stock item from database
                        //no need to check for result of RemoveItem()
			if ( Output( ask, "Do you really want to delete this item?" ) == QMessageBox::Cancel) break;
			DB->RemoveItem( item );
                        Output( message, tr("Item was succesfully removed") );
			break;
	}
}

void DataBaseWindow::GetItem( const ItemType& item )
{
	if ( LastPressed == 1 ) AddItem( item ); else
		if ( LastPressed == 2 ) EditItem( item );
}
